package org.example.cotroller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Result;
import org.example.entity.User;
import org.example.service.UserService;
import org.example.util.GetAccessKey;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class LoginController {

    @Resource
    private UserService userService;

    @PostMapping("/api/login")
    public Result login(@RequestBody User user) {
        log.info("管理登录:username={},password={}", user.getUsername(), user.getPassword());
        User login = userService.loginUser(user);
        if (login != null) {
            return Result.success(GetAccessKey.genAccessToken(user.getUsername()));
        }else return Result.error("账号密码错误");
    }
}
